<?php

namespace app\controllers\api;

use app\models\Loan;
use app\models\LoanRate;
use app\models\News;
use app\models\Transfer;
use app\models\User;
use Yii;
use yii\base\Exception;
use yii\base\UserException;
use yii\web\NotFoundHttpException;

class LoanController extends BaseController {
    // 贷款
    public function actionLoan() {
        $user = $this->check();
        $this->checkModify();
        $post = Yii::$app->request->post();
        $cycle = Yii::$app->request->post('cycle');
        $loanRate = LoanRate::findOne(['cycle'=>$cycle]);
        if(!$loanRate || $loanRate->country_id != $user->country_id) {
            throw new UserException('未选择合适的贷款周期');
        }
        $model = new Loan();
        $model->loadBeforeFilter($post, ['apply', 'num', ]);
        if(!($model->num > 0)) {
            throw new UserException('贷款金额错误');
        }
        if(!$model->apply) {
            throw new UserException('贷款用途未填写');
        }

        $model->user_id = $user->id;
        $model->country_id = $user->country_id;
        $model->cycle = $loanRate->cycle;
        $model->rate = $loanRate->loan_rate;
        $model->interest = $loanRate->loan_rate * $loanRate->cycle * $model->num;
        $model->status = Loan::STATUS_CREATE;
        $model->apply_time = date('Y-m-d H:i:s');
        $this->transaction(function() use ($model, $user) {
            $model->save();
            $this->log('贷款 - 申请', $model->id, 0);
        });
        return $this->renderSuccess([]);
    }

    // 贷款取消
    public function actionCancel() {
        $user = $this->check([User::ROLE_PRODUCER, User::ROLE_TRADER, ]);
        $this->checkModify();
        $id = Yii::$app->request->post('id');
        $model = Loan::findOne($id);
        if(!$model || $model->user_id != $user->id) {
            throw new UserException('不是本人的贷款无法操作');
        }
        if($model->status != 1) {
            throw new UserException('该状态无法取消');
        }
        $model->status = Loan::STATUS_CANCEL;
        $this->transaction(function() use ($model, $user) {
            $model->save();
            $this->log('贷款 - 取消', $model->id);
        });
        return $this->renderSuccess([]);
    }

    // 同意贷款
    public function actionAuth() {
        $user = $this->check([User::ROLE_GOV, ]);
        $this->checkModify();
        $id = Yii::$app->request->post('id');
        $model = Loan::findOne($id);
        if(!$model) {
            throw new UserException('贷款不存在');
        }
        if($model->country_id != $user->country_id) {
            throw new UserException('不是本国的贷款无法操作');
        }
        if($model->status != 1) {
            throw new UserException('该状态无法审核');
        }
        $model->status = Loan::STATUS_AUTH;
        $this->transaction(function() use ($model, $user) {
            $this->logForUser($model->user, '贷款 - 贷款审核通过', $model->id, $model->num);
            $this->log('贷款 - 同意', $model->id, -$model->num);
            $model->check_time = date('Y-m-d H:i:s');
            $model->save();
        });
        return $this->renderSuccess([]);
    }

    // 拒绝贷款
    public function actionReject() {
        $user = $this->check([User::ROLE_GOV, ]);
        $this->checkModify();
        $id = Yii::$app->request->post('id');
        $model = Loan::findOne($id);
        if(!$model) {
            throw new UserException('贷款不存在');
        }
        if($model->country_id != $user->country_id) {
            throw new UserException('不是本国的贷款无法操作');
        }
        if($model->status != 1) {
            throw new UserException('该状态无法审核');
        }
        $model->status = Loan::STATUS_REJECT;
        $model->check_time = date('Y-m-d H:i:s');
        $this->transaction(function() use ($model, $user) {
            $model->save();
            $this->log('贷款 - 拒绝', $model->id);
        });
        return $this->renderSuccess([]);
    }

    // 贷款偿还
    public function actionBack() {
        $user = $this->check([User::ROLE_PRODUCER, User::ROLE_TRADER, ]);
        $this->checkModify();
        $id = Yii::$app->request->post('id');
        $model = Loan::findOne($id);
        if(!$model || $model->user_id != $user->id) {
            throw new UserException('贷款不存在');
        }
        if($model->status != 3) {
            throw new UserException('该状态无法偿还');
        }
        $model->status = Loan::STATUS_BACK;
        $this->transaction(function() use ($model, $user) {
            $model->save();
            $this->log('贷款 - 偿还', $model->id, -$model->num);
            $this->log('贷款 - 偿还利息', $model->id, -$model->interest);
            $govUser = User::findOne([
                'country_id' => $user->country_id,
                'role' => User::ROLE_GOV,
            ]);
            if($govUser) {
                $this->logForUser($govUser, '贷款 - 偿还赚取利息', $model->id, $model->num + $model->interest);
            }
        });
        return $this->renderSuccess([]);
    }

    public function actionList() {
        $user = $this->check();
        $models = Loan::find()
            ->andWhere([
                'user_id' => $user->id
            ])
            ->all();
        $this->log('贷款 - 查询列表', '');
        return $this->renderSuccess([
            'list' => $models,
        ]);
    }

    public function actionRateList() {
        $user = $this->check();
        $models = LoanRate::find()
            ->andWhere([
                'country_id' => $user->country_id
            ])
            ->all();
        $this->log('贷款 - 查询利率', '');
        return $this->renderSuccess([
            'list' => $models,
        ]);
    }
}
